package bbs.service;

import bbs.exception.ServiceException;
import bbs.mapper.PostMapper;
import bbs.model.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @author ljg;
 * @create 2022-06-08 10:26
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, ServiceException.class})
public class PostService {

    @Autowired
    PostMapper postMapper ;

    @Autowired
    UserService userService;

    @Autowired
    TopicService topicService;

    /**
     * 根据 topicId（主题Id） 获取 post（帖子）
     * @param topicId
     * @return
     */
    public List<Post> getPostByTopicId(Integer topicId) {

        List<Post> postByTopicId = postMapper.getPostByTopicId(topicId);
        postByTopicId.forEach(post -> {
//            用来维护post 中的一对一关系
            post.setUser( userService.findById(post.getUserId()) );
            post.setTopic(topicService.findById(post.getTopicId()) );
        });

        return  postByTopicId ;
    }
}
